/**
 * Copyright (C), 2015-2024, XXX有限公司
 * FileName: DataValidate
 * Author:   mac
 * Date:     2024/3/22 16:09
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.finsiot.taos.test;

import com.finsiot.tools.taos.migration.bean.DataBase;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhangtianchang
 * @create 2024/3/22
 * @since 1.0.0
 */

@Slf4j
public class DataValidate {

    Connection connection;
    String filePath="/Volumes/HDD/test2";
    @Before
    public void createConnection(){
        DataBase dataBase = new DataBase("192.168.110.220", 6041, "test", "root", "taosdata","us",365);
        connection=dataBase.createConnection();
    }

    @Test
    public void validateLineNum() throws SQLException, IOException {

        String tableName="event_e402_trig";

        Statement statement= connection.createStatement();
        File file=new File(filePath+"/meta/"+tableName+".sql");

        executeDdl(file, statement);



        File dataFile=new File(filePath+"/data/"+tableName);
        for(File fileItem:dataFile.listFiles()) {
            String table=fileItem.getName().replace(".sql","");
            executeSql(fileItem, statement, table);
        }
        statement.close();
    }

    static int count(Statement statement,String database,String table) throws SQLException {
        ResultSet rs= statement.executeQuery("select count(1) from "+database+"."+table);rs.next();
        int count= rs.getInt(1);
        rs.close();
        return count;
    }

    static void executeSql(File sqlFile, Statement statement,String table) throws IOException {
        int line=0;
        FileReader fileReader=new FileReader(sqlFile);
        BufferedReader bufferedReader= new BufferedReader(fileReader);
        String tempLine;
        int curr=0;
        int last=0;
        while ((tempLine = bufferedReader.readLine()) != null) {
            if(tempLine.startsWith("#")){
                continue;
            }
            try {
                statement.execute(tempLine);
                line++;
            } catch (SQLException e) {
                log.error("execute sql:{} ,exception:{}",tempLine,e.getMessage());
            }

            try {
                curr= count(statement,"test",table);
                if(last==curr){
                    log.error("异常数据表：{}，行号：{}, SQL: {}",table,line,tempLine);
                    break;
                }
                last=curr;
            }catch (SQLException e) {
                log.error("execute sql exception:{}",e.getMessage());
            }

        }
        bufferedReader.close();
        fileReader.close();
    }

    static void executeDdl(File sqlFile, Statement statement) throws IOException {

        FileReader fileReader=new FileReader(sqlFile);
        BufferedReader bufferedReader= new BufferedReader(fileReader);
        String tempLine;
        while ((tempLine = bufferedReader.readLine()) != null) {
            if(tempLine.startsWith("#")){
                continue;
            }
            try {
                statement.execute(tempLine);
            } catch (SQLException e) {
                log.error("execute sql:{} ,exception:{}",tempLine,e.getMessage());
            }
        }
        bufferedReader.close();
        fileReader.close();
    }
}